/* command */
const { GIT_CURRENT_BRANCH_COMMAND, GIT_BRANCH_LIST_COMMAND, GIT_LOG_FIFTY_COUNT } = require('../model/command')
/* util */
const { command } = require('./common')

// 获取git当前分支
function getCurrentBranch() {
  return command(GIT_CURRENT_BRANCH_COMMAND).trim()
}

function getLocalBranches(isFilterCurrentBranch = false) {
  let branchesString = command(GIT_BRANCH_LIST_COMMAND)
  let branches = (
    branchesString
      .split('\n')
      .filter(branch => branch)
      .map(branch => branch.trim())
  )
  
  if (isFilterCurrentBranch) {
    // FIXME: 此处不严谨
    branches = branches.filter(branch => branch.indexOf('*') < 0)
  }
  
  return branches
}

// 获取git最近五十条记录
function getGitLogFiftyCount() {
  return command(GIT_LOG_FIFTY_COUNT)
}

exports.getGitLogFiftyCount = getGitLogFiftyCount
exports.getLocalBranches = getLocalBranches
exports.getCurrentBranch = getCurrentBranch

module.exports = {
  getCurrentBranch,
  getLocalBranches,
  getGitLogFiftyCount
}